<template>
  <div class="receiving">
    <van-nav-bar
            title="我的退货地址"
            right-text="删除选中"
            left-arrow
            @click-left="goBack"
            @click-right="deleteNow"
    />
    <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="onSelect"
    />
  </div>
</template>
<style>
.demo-nav {
  position: relative;
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  align-items: center;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  height: 56px;
  background-color: #fff;
}
.demo-nav__title {
  font-weight: 600;
  font-size: 17px;
  text-transform: capitalize;
}
.van-address-item__address{
  white-space: pre-wrap;
}
</style>
<script>
import { Toast } from 'vant';
import axios from "axios";

export default {
  data() {
    return {
      chosenAddressId: '',
      user_id:'',
      type:'3',
      list: [
        {
          id: '',
          name: '',
          tel: '',
          address: '151515',
          isDefault: false,
        },
      ],
    };
  },
  methods: {
    goBack(){
      this.$router.push("address-edit?user_id="+this.user_id);
    },
    onAdd() {
      this.$router.push("return-address-edit?user_id="+this.user_id);
    },
    onEdit(item, index) {
      // sessionStorage.setItem('address',JSON.stringify(this.list))
      this.$router.push("return-address-edit?id="+item.id+"&user_id="+this.user_id);

    },
    onSelect(item,index){
      this.chosenAddressId=index
    },
    deleteNow(){
      var _this = this
      if(_this.chosenAddressId==""){
        Toast('你还未选择任何地址!');
      }
      this.axios({
        method:"get",
        url:"/address_edit/deleteById/"+_this.chosenAddressId+"&"+_this.type+"&"+_this.user_id,
      }).then(function (rep){
        _this.list = rep.data.data.result_list[0]
        for(var i=0;i<rep.data.data.result_list[0].length;i++){
          if(_this.list[i].isDefault==1){
            _this.list[i].isDefault=true
          }
          else {
            _this.list[i].isDefault=false
          }
          if(rep.data.data.result_list[0][i].province!=rep.data.data.result_list[0][i].city){
            _this.list[i].address = rep.data.data.result_list[0][i].province+'\xa0\xa0'+rep.data.data.result_list[0][i].city+'\xa0\xa0'+rep.data.data.result_list[0][i].county+'\n'+rep.data.data.result_list[0][i].addressDetail
          }
          else {
            _this.list[i].address = rep.data.data.result_list[0][i].province+'\xa0\xa0'+rep.data.data.result_list[0][i].county+'\n'+rep.data.data.result_list[0][i].addressDetail
          }
        }
        if(rep.data.data.result_list[1]==1){
          Toast('删除成功');
        }
        else {
          Toast('删除失败');
        }
      })
    }
  },
  created() {
    var _this = this
    if(sessionStorage.getItem('reload')==1){
      alert('添加成功');
      sessionStorage.removeItem('reload')
      this.$router.go(0)
    }
    if(sessionStorage.getItem('reload')==2){
      alert('修改成功');
      sessionStorage.removeItem('reload')
      this.$router.go(0)
    }
    _this.user_id = _this.$route.query.user_id
    this.axios({
      method:"get",
      url:'/address_edit/addressByUserIdAndType/user_id='+_this.user_id+'&type='+_this.type,
    }).then(function (rep){
      _this.list = rep.data
      for(var i=0;i<rep.data.length;i++){

        if(_this.list[i].isDefault==1){
          _this.list[i].isDefault=true
        }
        else {
          _this.list[i].isDefault=false
        }
        if(rep.data[i].province!=rep.data[i].city){
          _this.list[i].address = rep.data[i].province+'\xa0\xa0'+rep.data[i].city+'\xa0\xa0'+rep.data[i].county+'\n'+rep.data[i].addressDetail
        }
        else {
          _this.list[i].address = rep.data[i].province+'\xa0\xa0'+rep.data[i].county+'\n'+rep.data[i].addressDetail
        }
      }
    })
  }
};
</script>

<style scoped>

</style>